package com.example.bankmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bankmanage.dto.rep.FetchLoginLogsReqDTO;
import com.example.bankmanage.dto.rep.FetchSystemLogsReqDTO;
import com.example.bankmanage.entity.*;
import com.example.bankmanage.mapper.LoginLogMapper;
import com.example.bankmanage.service.LoginLogService;
import org.springframework.stereotype.Service;

@Service
public class LoginLogServiceimpl extends ServiceImpl<LoginLogMapper, login_logs> implements LoginLogService {
    @Override
    public PageBean<login_logs> fetchLoginLogs(FetchLoginLogsReqDTO requestParam) {
        Page<login_logs> page = new Page<>(requestParam.getPage(), requestParam.getSize());

        // 构建查询条件
        LambdaQueryWrapper<login_logs> queryWrapper = new LambdaQueryWrapper<>();
//        System.out.println(requestParam.getCardNumber() != "");
        // 添加查询条件
        queryWrapper.eq(requestParam.getUserId() != null && requestParam.getUserId() != "", login_logs::getUserId, requestParam.getUserId())
                .eq(requestParam.getStatus() != null && requestParam.getStatus() != "",login_logs::getStatus, requestParam.getStatus())
                .eq(requestParam.getIpAddress() != null && requestParam.getIpAddress() != "",login_logs::getIpAddress, requestParam.getIpAddress())
                .ge(requestParam.getStartTime() != null,login_logs::getLoginTime, requestParam.getStartTime())
                .le(requestParam.getEndTime() != null, login_logs::getLoginTime, requestParam.getEndTime());

//        // 添加排序条件
//        if (StringUtils.isNotBlank(requestParam.getSortField()) && StringUtils.isNotBlank(requestParam.getSortOrder())) {
//            queryWrapper.orderBy(true, "asc".equalsIgnoreCase(requestParam.getSortOrder()),
//                    getSortField(requestParam.getSortField()));
//        } else {
//            // 默认按账单ID降序
//            queryWrapper.orderByDesc(bills::getBillId);
//        }
        page = baseMapper.selectPage(page, queryWrapper);
//        System.out.println(page.getPages());
//        System.out.println(page.getTotal());
        //这里的mybatis-plus里的count指令没有执行,要调
        PageBean<login_logs> pageBean = new PageBean<>();
        pageBean.setContent(page.getRecords());
        pageBean.setTotalElements(page.getTotal());
        // 执行查询
        return pageBean;
    }
}
